package com.dg11185.nearshop.activity.shop;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.libs.utils.date.DateUtils;
import com.dg11185.nearshop.activity.SubPageActivity;
import com.dg11185.nearshop.net.entity.Comment;
import com.dg11185.nearshop.net.request.GetMerchantCommentListRequest;
import com.dg11185.nearshop.net.response.GetMerchantCommentListResponse;
import com.dg11185.nearshop.net.response.NetStatus;
import com.dg11185.nearshop.view.adapter.CommmentsAdapter;
import com.dg11185.nearshop.view.xlistview.XListView;
import com.dg11185.nearshop.view.xlistview.XListView.IXListViewListener;
import com.wlper.lib.uitls.ViewUtils;
import com.wlper.lib.view.loading.LoadLayoutWithTweenAnim;

public class CommentListActivity extends SubPageActivity implements IXListViewListener {

    private XListView listView;

    private List<Comment> listDatas;

    private CommmentsAdapter listAdapter;

    private GetMerchantCommentListRequest request;

    private String merchantId;

    private int curPageNo = 1, total = 0, pageSize = 20;

    private LoadLayoutWithTweenAnim loadingLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop_comment_list);
        setSubPageTitle("评论列表");

        merchantId = getIntent().getStringExtra("merchantId");

        loadingLayout = (LoadLayoutWithTweenAnim) this.findViewById(R.id.shop_comment_loading_layout);
        request = new GetMerchantCommentListRequest(merchantId).add("orderName", "createTime");
        listView = (XListView) this.findViewById(R.id.shop_comment_list);
        listView.setPullRefreshEnable(false);
        listDatas = new ArrayList<Comment>();
        listAdapter = new CommmentsAdapter(this, listDatas);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new OnItemClickListener());
        listView.setXListViewListener(this);
        initCommentView();
    }

    @Override
    protected void onDestroy() {
        NetClient.httpCancel(mActivity, true);
        super.onDestroy();
    }
    private void initCommentView() {

        loadingLayout.onLoading();
        request.getParams().remove("chshSign");
        request.add("pageNumber", curPageNo + "").add("pageSize", pageSize + "").encryption();
        request.setActionListener(new GetMerchantCommentListRequest.ActionListener<GetMerchantCommentListResponse>() {
            @Override
            public void onSuccess(GetMerchantCommentListResponse result) {
                if (result.status == NetStatus.SUCCESS) {
                    loadingLayout.onLoadSuccess();
                    listView.setVisibility(View.VISIBLE);
                    listView.stopLoadMore();
                    listView.stopRefresh();
                    listView.setRefreshTime(DateUtils.getCurDateTime());
                    total = Integer.parseInt(result.total);
                    listDatas.addAll(result.comments);
                    listAdapter.notifyDataSetChanged();
                    if (curPageNo * pageSize > total) {
                        listView.setPullLoadEnable(false);
                    }
                } else {
                    onFailure(-1);
                }
            }

            @Override
            public void onFailure(int resultCode) {
                loadingLayout.onLoadFailed();
                listView.stopLoadMore();
                listView.stopRefresh();
            }
        });
        NetClient.httpGet(request);
    }

    class OnItemClickListener implements android.widget.AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int head = listView.getHeaderViewsCount();
            if (position < head || position >= (listDatas.size() + head)) {
                return;
            }
            Intent intent = new Intent(CommentListActivity.this, ReplyCommentActivity.class);
            intent.putExtra("mainComment", listDatas.get(position - head));
            startActivity(intent);
        }
    }

    @Override
    public void onRefresh() {
        initCommentView();
    }

    @Override
    public void onLoadMore() {
        if (curPageNo * pageSize >= total) {
            ViewUtils.showToast(CommentListActivity.this,R.string.no_data_anymore);
            listView.stopLoadMore();
        } else {
            curPageNo++;
            initCommentView();
        }
    }
}
